package com.orange.rd.sophia.android.sensorscharts.activity;


import com.orange.rd.sophia.android.sensorscharts.R;
import com.orange.rd.sophia.android.sensorscharts.SensorsChartsApplication;
import com.orange.rd.sophia.android.sensorscharts.conf.Properties;
import com.orange.rd.sophia.android.sensorscharts.tools.ApplicationMenu;
import com.orange.rd.sophia.android.sensorscharts.tools.SensorsDataManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

/**
 * Setup sensors
 * @author VLWB5512
 *
 */
public class SensorsSetupActivity extends Activity {
	public static final String TAG = "Sensors setup";
	Button mSaveBtn;
	Button mReturnBtn;
	LinearLayout layoutWS;

	@Override
	public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
		setTitle(TAG);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.setup);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		((TextView) findViewById(R.id.activityTitle)).setText(TAG);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		// Create an ArrayAdapter using the string array and a default spinner layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
		        R.array.sensors_delays, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);
		mSaveBtn = (Button) findViewById(R.id.ok_button);
		mReturnBtn = (Button) findViewById(R.id.cancel_button);
		mSaveBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				saveSetup();
			}
		});
		mReturnBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				goPrevious();
			}
		});
		// Retrieve previous setup
		SharedPreferences settings = getSharedPreferences(
				SensorsChartsApplication.PREFS, Context.MODE_PRIVATE);
		int sensorDelay = settings.getInt(getApplicationContext().getString(R.string.sensor_delay), SensorManager.SENSOR_DELAY_UI);
		// Initialize data in view
		switch (sensorDelay) {
		case SensorManager.SENSOR_DELAY_FASTEST:
			spinner.setSelection(0);
			break;
		case SensorManager.SENSOR_DELAY_GAME:
			spinner.setSelection(1);
			break;
		case SensorManager.SENSOR_DELAY_NORMAL:
			spinner.setSelection(2);
			break;
		case SensorManager.SENSOR_DELAY_UI:
			spinner.setSelection(3);
			break;
		}


	}
	
	private void goPrevious() {
		Intent k = new Intent();
		k.setClass(this, Properties.previousActivity);
		k.setAction(Intent.ACTION_VIEW);
		finish();
		startActivity(k);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		ApplicationMenu.onCreateOptionsMenu(this, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		ApplicationMenu.onPrepareOptionsMenu(this, menu);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		ApplicationMenu.onOptionsItemSelected(this, item);
		return true;
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goPrevious();
			return true;
		}
		return false;
	}

	private void saveSetup() {
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 		
		SharedPreferences settings = getSharedPreferences(
				SensorsChartsApplication.PREFS, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		int sensorDelay = SensorManager.SENSOR_DELAY_UI;
		switch (spinner.getSelectedItemPosition()) {
		case 0:
			sensorDelay = SensorManager.SENSOR_DELAY_FASTEST;
			break;
		case 1:
			sensorDelay = SensorManager.SENSOR_DELAY_GAME;
			break;
		case 2:
			sensorDelay = SensorManager.SENSOR_DELAY_NORMAL;
			break;
		case 3:
			sensorDelay = SensorManager.SENSOR_DELAY_UI;
			break;
		}
		editor.putInt(getApplicationContext().getString(R.string.sensor_delay), sensorDelay);
		Properties.sensorDelay = sensorDelay;

		if (SensorsDataManager.selectedData!=null)
			SensorsDataManager.selectedData.setDelay(sensorDelay);

		if (android.os.Build.VERSION.SDK_INT >= 9)
			editor.apply();
		editor.commit();
	}

}
